
#include "soundmanager.h"
#include "SimpleAudioEngine.h"
#include "strutil.h"
#include "bullet.h"


bool SoundManager::_isEfSoundOn = true;

bool SoundManager::isBackgroundOn = false;

BGMusicType SoundManager::bgid = BGMusicType::count;

const float SoundManager::_arrSoundDuration[] = {
	2.253f, //achievement
};

string SoundManager::_arrSoundFiles[] = {
		"achievement",
	};


void SoundManager::init(bool isSoundOn, bool needWaiting) {
	_isEfSoundOn = isSoundOn;

	for (int i = 0; i < (int)SoundId::count; ++i) {
		string s;
#if ANDROID
        s = "sound_android/" + _arrSoundFiles[i] + ".ogg";
#elif IOS
		s = "sound_ios/" + _arrSoundFiles[i] + ".mp3";
#else
		s = "sound_win/" + _arrSoundFiles[i] + ".wav";
#endif
		_arrSoundFiles[i] = s;

		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(s.c_str());
    }

	CocosDenshion::SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5f);

	isBackgroundOn=isSoundOn;

	CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5f);
}


void SoundManager::playUI(SoundId sid) {
    if (!_isEfSoundOn) {
        return;
    }
    
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(_arrSoundFiles[(int)sid].c_str());
}

void SoundManager::play(SoundId sid, bool forcePlay) {
    if (!_isEfSoundOn) {
        return;
    }
    
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(_arrSoundFiles[(int)sid].c_str());
}

void SoundManager::stop(int sid) {
    if (!_isEfSoundOn || sid==-1) {
        return;
    }

	CocosDenshion::SimpleAudioEngine::sharedEngine()->stopEffect(sid);
}

void SoundManager::stopAll() {
	CocosDenshion::SimpleAudioEngine::sharedEngine()->stopAllEffects();
}

void SoundManager::update(float ticks) {
    if (!_isEfSoundOn) {
        return;
    }


}

void SoundManager::setBgId(BGMusicType id) {
	bgid = id;
}

void SoundManager::gameBGStart() {
	if (!isBackgroundOn) {
		return;
	}

	switch (bgid) {
		default:
			break;
	}
}

void SoundManager::gameBGStop() {
	if (isBackgroundOn) {
		CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
	}
}


void SoundManager::setbgMusicPlay(bool play){
	isBackgroundOn = play;
}
